package com.algorithm.linesegment;

/**
 * @Author:DOWN
 * @Date:2022/5/5 16:15
 * @Comment:线段树
 */

public class SegmentTree<E> {
    //线段树
    private E[] tree;
    //数组
    private E[] data;
    //meager方法
    private ILineMeger<E> merger;

    /**
     * 初始化
     * @param arr 泛型元素组
     * @param merger merger自定义方法
     */
    public SegmentTree(E[] arr, ILineMeger<E> merger) {
        this.merger = merger;
        data = (E[]) new Object[arr.length];
        System.arraycopy(arr, 0, data, 0, arr.length);
        tree = (E[]) new Object[4 * arr.length];
        //构造线段树
        buildingSegmentTree(0, 0, data.length - 1);
    }

    /**
     * 构造线段树
     * @param treeIndex 根下标
     * @param l 左下标
     * @param r 右下标
     */
    private void buildingSegmentTree(int treeIndex, int l, int r) {
        if (l == r) {
            tree[treeIndex] = data[l];
            return;
        }
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        int mid = l + (r - l) / 2;
        //递归调用
        buildingSegmentTree(leftTreeIndex, l, mid);
        buildingSegmentTree(rightTreeIndex, mid + 1, r);
        //自定义方法计算 线段树集合
        tree[treeIndex] = merger.merger(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    /**
     * 获取个数
     * @return int
     */
    public int getSize() {
        return data.length;
    }

    /**
     * 获取元素值
     * @param index 下标
     * @return 泛型元素
     */
    public E get(int index) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Index is illegal");
        }
        return data[index];
    }

    /**
     * 获取左孩子下标
     * @param index 根下标
     * @return int
     */
    private int leftChild(int index) {
        return 2 * index + 1;
    }
    /**
     * 获取右孩子下标
     * @param index 根下标
     * @return int
     */
    private int rightChild(int index) {
        return 2 * index + 2;
    }

    /**
     * 查询线段树
     * @param queryL 左边界
     * @param queryR 右边界
     * @return 返回该线段区间集合值
     */
    public E query(int queryL, int queryR) {
        if (queryL < 0 || queryL >= data.length || queryR < 0 || queryR >= data.length
                || queryL > queryR) {
            throw new IllegalArgumentException("Index is illegal.");
        }
        return query(0, 0, data.length - 1, queryL, queryR);
    }

    /**
     * 查询线段树-递归方法
     * @param treeIndex 根下标
     * @param l 左边界
     * @param r 右边界
     * @param queryL 传入左边界
     * @param queryR 传入右边界
     * @return 返回该线段区间集合值
     */
    private E query(int treeIndex, int l, int r, int queryL, int queryR) {
        if (l == queryL && r == queryR) {
            return tree[treeIndex];
        }
        int mid = l + (r - l) / 2;
        int leftIndex = leftChild(treeIndex);
        int rightIndex = rightChild(treeIndex);
        if (queryL >= mid + 1) {
            return query(rightIndex, mid + 1, r, queryL, queryR);
        } else if (queryR <= mid) {
            return query(leftIndex, l, mid, queryL, queryR);
        }
        //此时 左右边界分隔在不同的线段区间中，需要分开查询
        E leftResult = query(leftIndex, l, mid, queryL, mid);
        E rightResult = query(rightIndex, mid + 1, r, mid + 1, queryR);
        //将分隔的 左右线段树区间合并
        return merger.merger(leftResult, rightResult);
    }

    /**
     * 根据索引更新线段树的值
     * @param index 索引
     * @param e 泛型元素
     */
    private void set(int index, E e) {
        if (index < 0 || index >= data.length) {
            throw new IllegalArgumentException("Index is illegal.");
        }
        set(0, 0, data.length - 1, index, e);
    }
    /**
     * 根据索引更新线段树的值-递归方法
     * @param treeIndex 根下标
     * @param l 左边界
     * @param r 右边界
     * @param index 索引
     * @param e 泛型元素
     */
    private void set(int treeIndex, int l, int r, int index, E e) {
        if (l == r) {
            tree[treeIndex] = e;
            return;
        }
        int mid = l + (r - l) / 2;
        int leftTreeIndex = leftChild(treeIndex);
        int rightTreeIndex = rightChild(treeIndex);
        if (index >= mid + 1) {
            set(rightTreeIndex, mid + 1, r, index, e);
        } else {
            set(leftTreeIndex, l, mid, index, e);
        }
        //每一次递归都需要 重新构建对应的线段区间。
        tree[treeIndex] = merger.merger(tree[leftTreeIndex], tree[rightTreeIndex]);
    }

    @Override
    public String toString() {
        StringBuilder res = new StringBuilder();
        res.append('[');
        for (int i = 0; i < tree.length; i++) {
            if (tree[i] != null) {
                res.append(tree[i]);
            } else {
                res.append("null");
            }
            if (i != tree.length - 1) {
                res.append(",");
            }
        }
        res.append(']');
        return res.toString();
    }
}
